const nodemailer = require('nodemailer');
const log = require('./log4js')('EMAIL');
const transporter = nodemailer.createTransport({
    service: '163',
    port: 465,
    secureConnection: false, // 使用了 SSL
    auth: {
        user: 'cander_server@163.com',//你的邮箱
        // 这里密码不是qq密码，是你设置的smtp授权码
        pass: 'li150815',
    }
});

function sendEmail(title, content, user = 'cander0815@163.com') {
    return new Promise((s, f) => {
        let mailOptions = {
            from: '"服务器" <cander_server@163.com>', // sender address
            to: user, // list of receivers
            subject: title, // Subject line
            html: content // html body
        };

        transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
                log.info('Email Message sent: fail');
                console.log(error);
                f(0);
                return;
            }
            log.info('Email Message sent: %s', info.messageId);
            s(1);
        });
    });
}

// const emailContent = `
//     <div style="margin: 0 auto; text-align: center;">
//         <h1>请注意错误过多</h1>
//         <a href="https://www.baidu.com">点击查看更多</a>
//     </div>
// `;
// sendEmail('warn error is so mach', emailContent);

module.exports = sendEmail;


